const config = require("./config");
function name1(str) {
  let items = (str || "").trim().split(/-|_/);
  let first = items[0];
  items.splice(0, 1);
  return (
    first +
    items
      .map((t) => {
        let first_char = t.substr(0, 1);
        let other_char = t.substr(1);
        return first_char.toUpperCase() + other_char;
      })
      .join("")
  );
}
function name2(name) {
  let first_char = name.substr(0, 1);
  let other_char = name.substr(1);
  return first_char.toUpperCase() + other_char;
}

function type(info) {
  if (info.type == "int") {
    // if (info.nullable == "NO") {
    //   return "int";
    // } else {
    return "Integer";
    // }
  } else if (info.type == "varchar") {
    return "String";
  } else if (info.type == "datetime") {
    return "java.util.Date";
  } else if (info.type == "date") {
    return "java.util.Date";
  } else if (info.type == "json") {
    return "String";
  } else if (info.type == "enum") {
    return "String";
  } else if (info.type == "tinyint") {
    // if (info.nullable == "NO") {
    //   return "boolean";
    // } else {
    return "Boolean";
    // }
  } else {
    debugger;
    return "未实现" + info.type;
  }
}

module.exports = {
  name: {
    first: name1,
    second: name2,
  },
  table: {
    start: (info) => {
      return `package ${config.package}.dao;

import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;

/**
 * ${info.comment}
 *
 * @author Code Generator at ${new Date().toLocaleString()}
 */
@Data
@Table(name = "\`${info.name}\`")
public class ${name2(name1(info.name))} implements Serializable {`;
    },
    end: () => {
      return "}";
    },
  },
  column: {
    comment: (info) => {
      return `    /**
     * ${info.comment.split(/\n|\r\n/).join("<br/>\n     * ")}
     */`;
    },
    annotation: (info) => {
      let content = [];
      if (info.paimary) content.push("    @Id");
      content.push(`    @Column(name = "\`${info.name}\`")`);
      if (info.paimary) content.push("    @GeneratedValue(strategy = GenerationType.IDENTITY)");
      return content.join("\n");
    },
    content: (info) => `    private ${type(info)} ${name1(info.name)};`,
    gset(info) {
      if (info.type == "json") {
        let name = name2(name1(info.name));
        return `

    /**
     * @return JSON对象
     *
     * @throws com.fasterxml.jackson.core.JsonProcessingException JSON反序列化异常
     */
    public java.util.HashMap<String, Object> get${name}Expand() throws com.fasterxml.jackson.core.JsonProcessingException {
        return new com.fasterxml.jackson.databind.ObjectMapper().readValue(this.${info.name}, java.util.HashMap.class);
    }

    /**
     * @param ${info.name} JSON对象
     *
     * @throws com.fasterxml.jackson.core.JsonProcessingException JSON序列化异常
     */
    public void set${name}Expand(java.util.HashMap<String, Object> ${info.name}) throws com.fasterxml.jackson.core.JsonProcessingException {
        this.${info.name} = new com.fasterxml.jackson.databind.ObjectMapper().writeValueAsString(${info.name});
    }
`;
      } else {
        return "\n";
      }
    },
  },
};
